<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="行业分类管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="分类名称" prop="typeName">
                <el-input v-model="formData.typeName"/>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存分类吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {industryTypeApi} from '@/api/user/industryTypeApi'
import {Common} from '@/utils/common'

export default {
    name: 'industryTypeUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: 0,
                parentId: 0,
                typeName: ''
            },
            rules: {
                typeName: [
                    {required: true, message: '请输入分类名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }

                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = 0
                    this.formData.parentId = data.parentId
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        industryTypeApi.edit(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',that.formData.parentId)
                            })
                    } else {
                        industryTypeApi.add(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh',that.formData.parentId)
                            })
                    }
                }
            })
        }
    }
}
</script>
